<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<style type="text/css">
	.right-operation ul{ list-style: none; }
	.right-operation ul li{ margin-top:10px;margin-bottom:10px; }
	.inputbtn{ height:35px;width:90px; }
	.td1{ width:10%; }
	.td2{ width:60%; }
	.td3{ width:10%; }
	.td4{ width:20%; }
	.self{ background-color:RGB(233,233,233); }
	.emz{ color:red; }
</style>

<div class="right-panel">
	<div class="header">
		<h1>文章编辑页面</h1>
	</div>
	
	<div class="right-operation">
		<h2>基本信息编辑</h2>
		<div>
			<ul>
				<li><input type="hidden" name="id" value="${param.id}" id="articleId"/></li>
				<li>标题1:<input type="text" value="${meta.title }" name="title" id="a_title"/></li>
				<li>标题s:<input type="text" value="${meta.stitle }" name="stitle" id="a_stitle"/></li>
				<li>标题2:<input type="text" value="${meta.ltitle }" name="ltitle" id="a_ltitle"/></li>
				<li>是否公开:公开<input type="radio" value="0" name="ops" checked="checked">私有<input type="radio" value="1" name="ops"></li>
			</ul>
		</div>
	</div>
	
	<div class="right-operation">
		<h2>Tag信息编辑</h2>
		<div>
			<ul>
				<c:forEach items="${tags }" var="a">
				<li><input type="checkbox" value="${a._id }" name="tag">${a.tag }</li>
				</c:forEach>
			</ul>
		</div>
	</div>	
	
	<div class="right-operation">
		<h2>正文编辑(html)</h2>
		<div>
			<textarea style="display:block; height:300px;width:95%;margin:5px auto 5px auto;" id="a_content">${article.content }</textarea>
			<input type="button" value="保存" class="inputbtn" data-ops="save" data-index="${param.id }"><input type="button" value="预览" class="inputbtn" data-ops="go" data-index="http://${server}/article/${stitle}">
		</div>
	</div>
	
</div>
<script type="text/javascript">
	(function(){

		var isEmpty = function(a){ return !a || a.length == 0; }

		var _ops = function(ops,index){
			//这里字段特别多
			var title = $("#a_title").val();
			var stitle = $("#a_stitle").val();
			var ltitle = $("#a_ltitle").val();
			var radio = $("input[name='ops']:checked").val();
			var tagVal = new Array();
			$("input[name='tag']:checked").each(function(){var t = $(this);tagVal.push(t.val());});
			var content = $("#a_content").val();
			var id = $("#articleId").val();
			var url = "/admin_t/update?p=article&t=" + ops;

			if( !id || id.length == 0 ){
				id = 0;
				url = "/admin_t/save?p=article&t=" + ops;
			}

			if( isEmpty(title) ){
				alert( "请填写标题!" );
				return ;
			}

			if( isEmpty(stitle) ){
				alert("请填写stitle!");
				return ;
			}

			if( isEmpty(radio) ){
				alert("请选择文章状态!");
				return ;
			}

			if( isEmpty(content) ){
				alert("请填写文章正文!");
				return ;
			}

			$.ajax({
				url:url,
				cache:false,
				type:"post",
				data:{title:title,stitle:stitle,ltitle:ltitle,content:content,ops:radio,tag:tagVal.join(","),id:id},
				dataType:"json",
				success:function(json){
					if( json && json.code == 200 && json.id != 0){
						window.location.href = "http://${server}/admin_t/index?p=newArticle&id=" + json.id;
					}else{
						alert("操作失败,错误代码:" + json.msg);
					}
				},
				error:function(){
					alert("操作失败!");
				}
			});
		}
		
		var init = function(){
			$(".inputbtn").each(function(){
				var t = $(this);
				var ops = t.attr("data-ops");
				var index = t.attr("data-index");
				if( !ops || ops.length == 0){
					return ;
				}
				if( "go" == ops ){
					t.click(function(){
						window.location.href = index;
					});
					return ;
				}
				t.click(function(){
					_ops(ops,index);
				});
			});
		}
		init();
	})();
</script>